package com.ikkong.biz.service.impl;

import com.google.common.collect.Lists;
import com.ikkong.biz.BizCst;
import com.ikkong.biz.excel.DeptOfMode;
import com.ikkong.biz.service.TeacherDeptService;
import com.ikkong.core.base.service.impl.BaseService;
import com.ikkong.core.constant.ConstCache;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.exception.BizException;
import com.ikkong.core.toolbox.ExcelStatistic;
import com.ikkong.system.model.Dept;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.ehcache.CacheKit;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import org.beetl.sql.core.BeetlSQLException;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.result.ExcelImportResult;
import org.jeecgframework.poi.exception.excel.ExcelImportException;

import java.io.File;
import java.io.FileOutputStream;
import java.sql.BatchUpdateException;
import java.util.List;

/**
 * Created by linyu on 2016/11/22.
 */
public class TeacherDeptServiceImpl extends BaseService<Dept> implements TeacherDeptService{
    private static final String filePath = "excel"+ File.separator+"dept"+File.separator+"dept_import.xlsx";


    @Override
    public String doImport(File file) throws BizException {
        PropKit.use("config.properties");
        String realpath = PathKit.getWebRootPath()+File.separator+ filePath;
        try (FileOutputStream fos= new FileOutputStream(realpath)){
            ImportParams params = new ImportParams();
            params.setNeedVerfiy(true);
            ExcelImportResult<DeptOfMode> result = ExcelImportUtil.importExcelVerify(
                    file,
                    DeptOfMode.class, params);
            result.getWorkbook().write(fos);

            //插入老师表
            List<Dept> clss = Lists.newArrayList();

            for (DeptOfMode cm:result.getList()) {
                if (StrKit.isBlank(cm.getErrorMsg())){
                    if (cm.getId().intValue()==1){
                        cm.setType(0);
                    }else {
                        cm.setType(BizCst.TEA_TYPE);
                    }
                    cm.setVersion(0);

                    clss.add(cm);

                }
            }
//            Blade.dao().executeUpdate(new SQLReady("truncate table tb_teacher"));
            Blade.dao().insertBatch(Dept.class,clss);

            CacheKit.removeAll(ConstCache.TEACHER_DEPT_CACHE);

            String reson = ExcelStatistic.statistic(result.getList().size(),clss.size(),filePath);
            return reson;
        } catch (Exception e) {
            if (e instanceof BatchUpdateException || e instanceof MySQLIntegrityConstraintViolationException || e instanceof BeetlSQLException){
                throw new BizException("不能重复导入相同的数据",e);
            }else if (e instanceof ExcelImportException){
                throw new BizException(e.getMessage(),e);
            }
            throw new BizException("导入失败",e);
        }finally {
            file.delete();
        }
    }

}
